//
//  WBWelcomeViewController.swift
//  Weibo
//
//  Created by xu on 2017/3/22.
//  Copyright © 2017年 xuantong. All rights reserved.
//

import UIKit
import SDWebImage


private let iconW: CGFloat = 85
private let bottomMargin: CGFloat = 180

class WBWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()

           }
    
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.startAnimation()
    }
    

    private func setupUI() {
        //设置背景颜色
        self.view.backgroundColor = UIColor.white
        self.view.addSubview(iconView)
        self.view.addSubview(welcomeLabel)

        welcomeLabel.alpha = 0
        iconView.layer.cornerRadius = iconW / 2
        iconView.layer.masksToBounds = iconView.layer.cornerRadius > 0

        iconView.sd_setImage(with: WBUserAccountViewModel.shared.headURL)

        
        //添加约束
        iconView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-bottomMargin)
            make.size.equalTo(CGSize(width: iconW, height: iconW))
            
        }
        
        welcomeLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp.bottom).offset(20)

        }
        
        
    }
    
    
    //头像,文字
    private lazy var iconView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "avatar_default_big"))
    private lazy var welcomeLabel: UILabel = UILabel(title: "欢迎归来", fontSize: 15, textColor: UIColor.darkGray)
    
    //加动画效果,将头像上移
    private func startAnimation() {
        
        let offset = -(ScreenHeight - bottomMargin - iconW)
        
        self.iconView.snp.updateConstraints { (make) in
            make.bottom.equalTo(self.view).offset(offset)
            
        }
        
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 5, options: [], animations: {
            //强制刷新
            self.view.layoutIfNeeded()

        }) { (_) in
            
            
            UIView.animate(withDuration: 0.8, animations: {
                self.welcomeLabel.alpha = 1
            }, completion: { (_) in
                //跳转到首页 (tabBar控制器)
                // 发送通知
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: SWITCHROOTVIEWCONTROLLERNOTI), object: "welcomeVc")            })
        }
        
        
    }
    

    
    

}
